package com.example.itcacp.controller;

import com.example.itcacp.dto.response.Result;
import com.example.itcacp.entity.Message;
import com.example.itcacp.entity.Message.MessageType;
import com.example.itcacp.service.MessageService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/notice")
public class NoticeController {

    @Resource
    private MessageService messageService;


    // 添加公告
    @PostMapping("/addNotice")
    public Result addNotice(@RequestBody Message message) {
        System.out.println (message);
        messageService.saveMessage ( message );
        return Result.success ();
    }

    // 获取公告列表（管理员）
    @GetMapping("/list")
    public Result getNoticeList(@RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "10") Integer pageSize,
                                @RequestParam(required = false) String content) {
        PageInfo<Message> messagePage = messageService.getMessageByType(pageNum, pageSize, content, MessageType.SYSTEM);
        // 只返回系统消息类型的公告
        return Result.success (messagePage);
    }

    // 删除公告
    @DeleteMapping("/delete/{messageId}")
    public Result deleteNotice(@PathVariable Long messageId) {
        messageService.deleteMessage(messageId);
        return Result.success ();
    }

    // 获取用户公告列表
    @GetMapping("/userList")
    public List<Message> getUserNoticeList(@RequestParam Long userId) {
        // 返回系统消息类型的公告，并标记用户是否已读
        return messageService.getUserNotices(userId);
    }

    // 标记公告为已读
    @PostMapping("/markRead/{id}")
    public void markNoticeAsRead(@PathVariable Long id) {
        messageService.markMessageAsRead(id);
    }
}